package uestc.lj.gulishop.member.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import uestc.lj.common.exception.ErrorCode;
import uestc.lj.common.utils.PageUtils;
import uestc.lj.common.utils.R;
import uestc.lj.gulishop.member.entity.MemberEntity;
import uestc.lj.gulishop.member.exception.PhoneExistException;
import uestc.lj.gulishop.member.service.MemberService;
import uestc.lj.gulishop.member.vo.MemberLoginVO;
import uestc.lj.gulishop.member.vo.MemberRegisterVO;
import uestc.lj.gulishop.member.vo.SocialUser;

import java.util.Arrays;
import java.util.Map;


/**
 * 会员
 *
 * @author crazlee
 * @email peterlee0@163.com
 * @date 2021-05-21 19:16:56
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;


    @PostMapping("/oauth2/login")
    public R weiboLogin(@RequestBody SocialUser socialUser) throws Exception {
        MemberEntity memberEntity = memberService.login(socialUser);
        if (memberEntity != null) {
            return R.ok().setData(memberEntity);
        } else {
            return R.error(ErrorCode.LOGIN_FAILED.getCode(), ErrorCode.LOGIN_FAILED.getMessage());
        }
    }

    @PostMapping("/login")
    public R login(@RequestBody MemberLoginVO vo) {
        MemberEntity memberEntity = memberService.login(vo);
        if (memberEntity == null) {
            return R.error(ErrorCode.LOGIN_FAILED.getCode(), ErrorCode.LOGIN_FAILED.getMessage());
        }
        return R.ok().setData(memberEntity);
    }

    @PostMapping("/regist")
    public R regist(@RequestBody MemberRegisterVO vo) {
        try {
            memberService.regist(vo);
        } catch (PhoneExistException exception) {
            return R.error(ErrorCode.PHONE_ALREAD_EXIST.getCode(), ErrorCode.PHONE_ALREAD_EXIST.getMessage());
        }
        return R.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    // @RequiresPermissions("member:member:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    // @RequiresPermissions("member:member:info")
    public R info(@PathVariable("id") Long id) {
        MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //  @RequiresPermissions("member:member:save")
    public R save(@RequestBody MemberEntity member) {
        memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //  @RequiresPermissions("member:member:update")
    public R update(@RequestBody MemberEntity member) {
        memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("member:member:delete")
    public R delete(@RequestBody Long[] ids) {
        memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
